package com.servlet.form;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Format {

	/**
	 * verifie le champ de l'email
	 * 
	 * @param email
	 * @return vrai l'email est valide
	 */
	public static boolean verifyEmail(String email) {
		if (email == null || email == "")
			return false;

		int size = email.length();
		if (size < 8)
			return false;

		int numAt = 0;
		boolean flag = false;
		int numPoint = 0;

		for (int i = 0; i < email.length(); i++) {
			if (email.charAt(i) == '@') {
				if (numAt != 0)
					return false;
				numAt++;
				flag = true;
			}
			if (email.charAt(i) == '.' && flag) {
				if (numPoint != 0)
					return false;
				numPoint++;
			}

		}
		if (numAt == 1 && numPoint == 1)
			return true;
		else
			return false;
	}

	/**
	 * verifie le champ du mot de passe
	 * 
	 * @param passwd
	 * @return vrai le mot de passe est valide
	 */
	public static boolean verifyPasswd(String passwd) {
		if (passwd == null || passwd == "")
			return false;

		int size = passwd.length();
		if (size < 8 || size > 20)
			return false;

		boolean flagChiffre = false;
		boolean flagMajus = false;
		for (int i = 0; i < passwd.length(); i++) {
			if (passwd.charAt(i) >= 'A' && passwd.charAt(i) <= 'Z')
				flagMajus = true;
			if (passwd.charAt(i) >= '0' && passwd.charAt(i) <= '9')
				flagChiffre = true;
			if (flagChiffre && flagMajus)
				break;
		}

		if (flagChiffre && flagMajus)
			return true;
		else
			return false;
	}

	/**
	 * verifie le champ du nom ou prénom
	 * 
	 * @param name
	 * @return vrai le champ du nom ou prénom est valide
	 */
	public static boolean verifyName(String name) {
		if (name == null || name == "" || name.contains("'") )
			return false;

		int size = name.length();

		if (size < 2 || size > 40)
			return false;
		else
			return true;
	}

	public static boolean verifyNumero(String numero) {
		try {
			Integer.parseInt(numero);
		} catch (Exception e) {
			return false;
		}

		int size = numero.length();

		if (size < 1 || size > 40)
			return false;
		else
			return true;
	}

}
